package com.pic.mgt.syushin;

import com.pic.mgt.executer.UnitDispatchException;
import com.pic.mgt.executer.UnitDispatcher;
import com.pic.mgt.executer.UnitExecuter;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.syushin.unit.BucketOkSyushinMessageUnit;
import com.pic.mgt.syushin.unit.RealTargetOkSyushinMessageUnit;
import com.pic.mgt.unit.Unit;
import com.pic.mgt.unit.operation.MessageUnitOperation;
import com.pic.mgt.unit.operation.UnitOperationException;
import com.pic.mgt.utils.LogUtils;

public final class SyushinMessageUnitDispatcher extends UnitDispatcher {
	
	private UnitExecuter realSyushinMessageUnitExecuter;
	private UnitExecuter bucketSyushinMessageUnitExecuter;

	@Override
	protected void execute(Unit unit) {
		if (RealTargetOkSyushinMessageUnit.class.isInstance(unit)) {
			// real 集信 message
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching unit '{}' to real syushin message unit executer.",
						unit.getUnitName());
			}
			// ********************** Log **********************
			
			this.realSyushinMessageUnitExecuter.execute(unit);
		}
		else if (BucketOkSyushinMessageUnit.class.isInstance(unit)) {
			// bucket 集信 message

			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching unit '{}' to bucket syushin message unit executer.",
						unit.getUnitName());
			}
			// ********************** Log **********************
			
			this.bucketSyushinMessageUnitExecuter.execute(unit);
		}
		else {
			try {
				throw new UnitDispatchException(
						"SUDA-310 Dispatcher can't find matched executer for message unit '" +
						unit.getClass().getName() + "'.");
			} catch (UnitDispatchException e) {
				// ********************** Log **********************
				IgxLog.messageLog.error(this, e.getMessage());
				// ********************** Log **********************
			}
		}
	}
	
	// --- getter and setter ---

	public void setRealSyushinMessageUnitExecuter(
			UnitExecuter realSyushinMessageUnitExecuter) {
		this.realSyushinMessageUnitExecuter = realSyushinMessageUnitExecuter;
	}

	public void setBucketSyushinMessageUnitExecuter(
			UnitExecuter bucketSyushinMessageUnitExecuter) {
		this.bucketSyushinMessageUnitExecuter = bucketSyushinMessageUnitExecuter;
	}
}
